package controller.servlet.user;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Company;
import model.User;
import controller.dao.Dao_Company;
import controller.dao.Dao_User;

public class register extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Dao_Company daoC = new Dao_Company();
	    Company company = daoC.list(1);
	    if(company == null){
	    	company = new Company();
	    	company.setCnpj("1234567890");
	    	company.setCorporateName("ShoppingCart.Com, Inc.");
	    }
	    getServletContext().setAttribute("company", company);
	    
		String name = request.getParameter("name");
		String login = request.getParameter("login");
		String email = request.getParameter("email");
		String pass	= request.getParameter("pass");
		
		String profiles = "";
		String [] profile = request.getParameterValues("profile");
		
		for(String p: profile)
		{
			profiles+=p+","; 
		}
		
		Company company2 = (Company) getServletContext().getAttribute("company");
		
		Dao_User daoe = new Dao_User();
		User user = new User();
	
		user.setName(name);
		user.setLogin(login);
		user.setPassword(pass);
		user.setEmail(email);
		user.setProfileType(profiles);
		user.setCompany(company2);
		
		daoe.save(user);
		response.getWriter().print("1");
	}

}
